package com.egao.common.publicly.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.company.constants.AttachmentConstants;
import com.egao.common.module.company.entity.Attachment;
import com.egao.common.module.company.entity.Position;
import com.egao.common.module.company.entity.Recruit;
import com.egao.common.module.company.service.AttachmentService;
import com.egao.common.module.company.service.PositionService;
import com.egao.common.module.push.mapper.PushMsgMapper;
import com.egao.common.module.system.entity.College;
import com.egao.common.module.system.service.CollegeService;
import com.egao.common.publicly.entity.PubliclyCareerVO;
import com.egao.common.publicly.mapper.PubliclyRecruitMapper;
import com.egao.common.publicly.service.PubliclyMatchingService;
import com.egao.common.publicly.service.PubliclyRecruitService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 招聘信息接口实现类
 *
 * @author pyx
 */
@Service
public class PubliclyRecruitServiceImpl extends ServiceImpl<PubliclyRecruitMapper, Recruit> implements PubliclyRecruitService {
    @Autowired
    private PositionService positionService;
    @Autowired
    private CollegeService collegeMajorService;
    @Autowired
    private AttachmentService attachmentService;

    @Override
    public List<PubliclyCareerVO> listPage(PageParam<PubliclyCareerVO> pageParam) {
        return baseMapper.listPage(pageParam);
    }

    @Override
    public Recruit getId(Integer id) {
        QueryWrapper<Recruit> recruitQueryWrapper = new QueryWrapper<>();
        recruitQueryWrapper.eq("state", "1").eq("invalid", 0).isNotNull("valid_time").eq("id", id);
        Recruit recruit = baseMapper.selectOne(recruitQueryWrapper);

        if (recruit == null) {
            return new Recruit();
        }

        //插入附件集合
        List<Attachment> attachmentList = attachmentService.getAttachByBusinessId(recruit.getId(), AttachmentConstants.TYPE_RECRUIT);
        if (attachmentList.size() == 0) {
            recruit.setAttachmentList(null);
        } else {
            recruit.setAttachmentList(attachmentList);
        }

        //更新点击量
        if (recruit.getHits() == null) {
            recruit.setHits(0);
        }
        recruit.setHits(recruit.getHits() + 1);
        baseMapper.updateById(recruit);

        if (!StrUtil.isBlank(recruit.getPositionIds())) {
            List<Position> listPosition = positionService.getPositionByIds(recruit.getPositionIds());

            for (Position position : listPosition) {
                if (!StrUtil.isBlank(position.getMajor())) {
                    String[] majors = position.getMajor().split(",");
                    ArrayList<String> majorNameList = new ArrayList<>();

                    for (String majorId : majors) {
                        College major = collegeMajorService.getByCode(majorId);

                        if (major != null) {
                            majorNameList.add(major.getName());
                        }

                    }

                    position.setMajorName(StringUtils.join(majorNameList, ","));
                }
            }

            if (listPosition.size() == 0) {
                recruit.setListPosition(null);
            } else {
                recruit.setListPosition(listPosition);
            }

        }

        return recruit;
    }

    @Override
    public Integer allHists(HashMap<String, String> createTimeMap, Integer type) {
        return baseMapper.allHists(createTimeMap, type);
    }
}
